﻿using System;
using System.Collections.Generic;
using System.Text;

namespace IllustratedCsharp7_ch8
{
    class Employee { 
        public string LastName;             //调用字段0
        public string FirstName;            //调用字段1
        public string CityOfBirth;          //调用字段2

        public string this[int index]       //索引器声明
        {
            set                             //set访问器声明
            {
                switch (index)
                {
                    case 0:
                        LastName = value;
                        break;
                    case 1:
                        FirstName = value;
                        break;
                    case 2:
                        CityOfBirth = value;
                        break;
                    default:
                        throw new ArgumentOutOfRangeException("index");
                }
            }
            get                             //get访问器声明
            {
                switch (index)
                {
                    case 0: return LastName;
                    case 1: return FirstName;
                    case 2: return CityOfBirth;
                    default:
                        throw new ArgumentOutOfRangeException("index");
                }
            }
        }
    }

    class chapt7_17_Indexer
    {
        static void _Main()
        {
            Employee emp1 = new Employee
            {
                LastName = "Doe",
                FirstName = "Jane",
                CityOfBirth = "Dallas"
            };
            Console.WriteLine("{0}",emp1.LastName);
            Console.WriteLine("{0}", emp1.FirstName);
            Console.WriteLine("{0}", emp1.CityOfBirth);



            Employee emp2 = new Employee();
            emp2[0] = "Jonn";
            emp2[1] = "Casper";
            emp2[2] = "NewYok";
            Console.WriteLine("{0}", emp2[0]);
            Console.WriteLine("{0}", emp2[1]);
            Console.WriteLine("{0}", emp2[2]);
        }
    }
}
